package com.dst.smart.handler.disposer;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import com.dst.smart.app.MainActivity;
import com.dst.smart.handler.entity.SemanticResult;
import com.dst.smart.handler.entity.ServiceType;
import com.dst.smart.player.entity.SongPlayInfo;
import com.dst.smart.threadpool.ThreadPoolTaskSong;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 开放问答
 * 
 * author <a href="http://www.xfyun.cn">讯飞开放平台</a>
 * date 2016年6月21日 下午4:29:54
 *
 */
public class OpenQADisposer extends Disposer {

	public OpenQADisposer(Context context) {
		super(context);
	}

	@Override
	public void disposeResult(final SemanticResult result) {
		try {
			String text = result.getAnswerText();
			Log.e("text","OpenQA"+text);
			if(!TextUtils.isEmpty(text)){
				getPlayController().playURLList(result.getUUID(),null);
				getPlayController().playText(result.getUUID(), text);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public boolean canHandle(ServiceType type){
		return (type == ServiceType.OPENQA);
	}
	/*
	private void parseMusicJson(SemanticResult semanticResult) {

		try {
			JSONArray result = semanticResult.getResult();
			
			if (null != result && result.length() > 0) {
				for (int i = 0; i < result.length(); i++) {

					JSONObject songInfoJson = result.getJSONObject(i);
					String singernames = songInfoJson.getString("singernames");

						ThreadPoolTaskSong songThread = new ThreadPoolTaskSong(	singernames,
								singernames, "music",
								songInfoJson.optString("audiopath"),
								songInfoJson.optString("songname"));
						MainActivity.activity.poolManager
								.addAsyncTask(songThread);
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	private List<SongPlayInfo> parseMusicList(SemanticResult semanticResult) {

		try {
			JSONArray result = semanticResult.getResult();

			if (null != result && result.length() > 0) {
				List<SongPlayInfo> songPlayInfos = new ArrayList<SongPlayInfo>();

				for (int i = 0; i < result.length(); i++) {

					JSONObject songInfoJson = result.getJSONObject(i);


					SongPlayInfo playInfo = new SongPlayInfo();

					playInfo.setSongName(songInfoJson.optString("songname"));
					playInfo.setPlayUrl(songInfoJson.optString("audiopath"));

					JSONArray singernames = songInfoJson.optJSONArray("singernames");
					if (singernames != null && singernames.length() > 0) {
						StringBuffer singerName = new StringBuffer();
						for (int j = 0; j < singernames.length(); j++) {
							singerName.append(singernames.optString(j) + ",");
						}

						playInfo.setSingerName(singerName.toString());
					}
					if(i == 0){
						playInfo.setAnswerText(semanticResult.getAnswerText());
					}

					songPlayInfos.add(playInfo);
				}
				return songPlayInfos;
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
	}
	*/
}
